﻿using System;
using System.Data;

namespace AssettDatabase.DAL
{
    class Vendor
    {
        private string sqltext = "";

        public bool Add(string Vendor_Name, string Vendor_Support_Phone, string Vendor_Support_Email, string Vendor_Notes)
        {
            try
            {
                DataAccess.DAO mDAO = new DataAccess.DAO();
                sqltext = "INSERT INTO Vendors (Vendor_Name, Vendor_Support_Phone, Vendor_Support_Email, Vendor_Notes, Vendor_Status)" +
                            "VALUES (@Vendor_Name, @Vendor_Support_Phone, @Vendor_Support_Email, @Vendor_Notes, 1);";

                mDAO.AddParameter("@Vendor_Name", Vendor_Name);
                mDAO.AddParameter("@Vendor_Support_Phone", Vendor_Support_Phone);
                mDAO.AddParameter("@Vendor_Support_Email", Vendor_Support_Email);
                mDAO.AddParameter("@Vendor_Notes", Vendor_Notes);
            
                
                mDAO.ExecuteNonQuery(sqltext);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;
        }

        public bool Update(string Vendor_Name, string Vendor_Support_Phone, string Vendor_Support_Email, string Vendor_Notes, string Vendor_ID)
        {
            try
            {
                DataAccess.DAO mDAO = new DataAccess.DAO();
                sqltext =   "UPDATE Vendor SET Vendor_Name=@Vendor_Name," +
                            "Vendor_Support_Phone=@Vendor_Support_Phone," +
                            "Vendor_Support_Email=@Vendor_Support_Email," +
                            "string Vendor_Notes=@string Vendor_Notes," +
                            "WHERE Vendor_ID=@Vendor_ID";

                mDAO.AddParameter("@Vendor_Name", Vendor_Name);
                mDAO.AddParameter("@Vendor_Support_Phone", Vendor_Support_Phone);
                mDAO.AddParameter("@Vendor_Support_Email", Vendor_Support_Email);
                mDAO.AddParameter("@Vendor_Notes", Vendor_Notes);
                mDAO.AddParameter("@Vendor_ID", Vendor_ID);
               
                mDAO.ExecuteNonQuery(sqltext);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return true;
        }

        public DataTable GetVendor()
        {
            try
            {
                DataAccess.DAO mDAO = new DataAccess.DAO();
                sqltext = "SELECT Vendor_ID, Vendor_Name, Vendor_Support_Email, Vendor_Support_Phone, Vendor_Notes, " +
                            "CASE WHEN Vendor_Status = 1 THEN Vendor_Name ELSE CONCAT(Vendor_Name, ' (retired)') END AS Vendor_Name " +
                            "FROM Vendors " +
                            "ORDER BY Vendor_Name";

                return mDAO.ExecuteSelectQuery(sqltext);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
